#ifndef _GLTEXT_H_
#define _GLTEXT_H_

#include <math.h>
#include <stdio.h>
#include <stdarg.h>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//#include <winuser.h>
#include <tchar.h>
#endif

#include <gl/gl.h>

extern float white[4];
extern float gray[4];

class GLText
{
public:
	GLText();
	~GLText();

	int Load(char *name);
	int LoadEx(char *name, int row, int col);
	void Init(GLuint texture);
	void Init(GLuint texture,int row, int col);
	void Draw(int xpos, int ypos, char *s, ...);
	void Region(int x, int y, int w, int h)
	{	region = 1; regionX = x; regionY = y - h; regionW = w; regionH = h;}
	void Size(int sze) {size = sze;}
	void Shadow(void) {shadow = 1;}
	void Gradient(void) {gradient = 1;}
	void Color3f(float r, float g, float b)
	{fgColor[0] = r; fgColor[1] = g; fgColor[2] = b; fgColor[3] = 1.0;}
	void Color4f(float r, float g, float b, float a)
	{fgColor[0] = r; fgColor[1] = g; fgColor[2] = b; fgColor[3] = a;}
	void Color3fv(float *clr)
	{fgColor[0] = clr[0]; fgColor[1] = clr[1]; fgColor[2] = clr[2]; fgColor[3] = 1.0;}
	void Color4fv(float *clr)
	{fgColor[0] = clr[0]; fgColor[1] = clr[1]; fgColor[2] = clr[2]; fgColor[3] = clr[3];}
	void ShadowColor3f(float r, float g, float b)
	{bgColor[0] = r; bgColor[1] = g; bgColor[2] = b; bgColor[3] = 1.0;}
	void ShadowColor4f(float r, float g, float b, float a)
	{bgColor[0] = r; bgColor[1] = g; bgColor[2] = b; bgColor[3] = a;}
	void ShadowColor3fv(float *clr)
	{bgColor[0] = clr[0]; bgColor[1] = clr[1]; bgColor[2] = clr[2]; bgColor[3] = 1.0;}
	void ShadowColor4fv(float *clr)
	{bgColor[0] = clr[0]; bgColor[1] = clr[1]; bgColor[2] = clr[2]; bgColor[3] = clr[3];}
	void GradientColor3f(float r, float g, float b)
	{gdColor[0] = r; gdColor[1] = g; gdColor[2] = b; gdColor[3] = 1.0;}
	void GradientColor4f(float r, float g, float b, float a)
	{gdColor[0] = r; gdColor[1] = g; gdColor[2] = b; gdColor[3] = a;}
	void GradientColor3fv(float *clr)
	{gdColor[0] = clr[0]; gdColor[1] = clr[1]; gdColor[2] = clr[2]; gdColor[3] = 1.0;}
	void GradientColor4fv(float *clr)
	{gdColor[0] = clr[0]; gdColor[1] = clr[1]; gdColor[2] = clr[2]; gdColor[3] = clr[3];}


protected:
	static int fontBlockCol;
	static int fontBlockRow;
	static int fontGetModes; 
	static int fontRestoreModes; 
	static const int fontMaxLen;
	static int fontTabSpace;
	static float fontITOF;
	static int fontItalic;

	float fgColor[4];   /* foreground color, default white */
	float gdColor[4];   /* gradient color, default gray */
	float bgColor[4];   /* background color, default gray */
	int size;           /* size of text, default 12 */
	int shadow;         /* shadow text? default 0 */
	int gradient;       /* gradient? default 0 */
	int italic;         /* italic amount, default 0 */
	int bold;           /* bold text? */
	int region;         /* do we have a text region */
	int regionX;        /* lower left x */
	int regionY;        /* lower left y */
	int regionW;        /* text region w */
	int regionH;        /* text region h */
	float tIncX;        /* used for texture coords, x axis amount to move */
	float tIncY;        /* used for texture coords, y axis amount to move */
	int blockRow;       /* characters per row */
	int blockCol;       /* characters per col */
	GLuint texId;		/* texture id */

	void ColorCopy(float *dest, float *src)
	{	dest[0] = src[0]; dest[1] = src[1]; dest[2] = src[2]; dest[3] = src[3];	}
	void Reset();
	void DrawChar(char c, int x, int y, int size, int shadow);
	void RenderChar(char c, int x, int y, int size);
	void ScissorNormal(int xpos, int ypos, int tabs, int carrage, int size, int len);
	void ScissorTextRegion(void)
	{	glScissor(regionX, regionY, regionW, regionH);	}
	void ForeColorReset(void) {	ColorCopy(fgColor, white);}
	void ShadowColorReset(void) {	ColorCopy(bgColor, gray);}
	void GradientColorReset(void) {	ColorCopy(gdColor, gray);}
	int SlashParser (char *buffPtr, int *x, int *y);
	void WalkString(char *buffPtr, int xpos, int ypos, int *vPort);
	int SetColorFromToken(char *s);
	int ItalicsMode(char *s);
	int BoldMode(char *s);
	int GetCharHits(char *s, char f);
	void MakeMap(void);
	void SetModes(int state);
};


#endif